package com.neusoft.service.impl;

import com.neusoft.mapper.VirtualwalletMapper;
import com.neusoft.po.Virtualwallet;
import com.neusoft.service.VirtualwalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
@Service
public class VirtualwalletServiceImpl implements VirtualwalletService {

    @Autowired
    private VirtualwalletMapper virtualwalletMapper;

    @Override
    public Virtualwallet getVirtualwalletByUserId(String userId) {
        return virtualwalletMapper.GetVirtualwalletByUserId(userId);
    }

    @Override
    public int saveVirtualwallet(String userId) {
        return virtualwalletMapper.saveVirtualwallet(userId);
    }

    @Override
    public int rechargeByUserId(String userId,BigDecimal add_balance){
        //从其他渠道将款转到elm下，具体内容省略
        if(add_balance.compareTo(BigDecimal.ZERO)<0){
            return -2;
        }
        BigDecimal balance = virtualwalletMapper.GetVirtualwalletByUserId(userId).getBalance();
        balance = balance.add(add_balance);
        return virtualwalletMapper.updateVirtualwallet(userId,balance);
    }

    //public int payoutByUserId(String userId, BigDecimal sub_balance);
    @Override
    public int payoutByUserId(String userId, BigDecimal sub_balance) {
        //提现
        if(sub_balance.compareTo(BigDecimal.ZERO)<0){
            return -2;
        }
        BigDecimal balance = virtualwalletMapper.GetVirtualwalletByUserId(userId).getBalance();
        balance = balance.subtract(sub_balance);
        return virtualwalletMapper.updateVirtualwallet(userId,balance);
    }
}
